package net.cardgame.orcalecard.utils;

import java.lang.reflect.Type;

import net.cardgame.orcalecard.bean.SettingApp;
import net.cardgame.orcalecard.pref.SecurePreferences;
import android.content.Context;
import android.os.Environment;
import android.os.StatFs;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

public class ConstantValue {

	public static final int RESULT_ACTIVITY = 7;
	public static final int SAVE_HISTORY_ACTIVITY = 8;
	public static final String CONFIG_DECKBEAN_KEY = "list_deckbean";
	public static final int CURRENT_API_VERSION = android.os.Build.VERSION.SDK_INT;
	// base url cho API
	public static final String BASE_URL = "http://www.jma-inc.jp/appdata/OracleCard/";
	// key history shared reference
	public static final String HISTORY_KEY = "history_sharedreference";
	// key setting app shared reference
	public static final String SETTING_KEY = "setting_sharedreference";
	// url của file chứa danh sách deck
	public static final String LIST_DECK_SUB_URL = "ConfigAndroid.xml";
	// key sharedreference để kiểm tra đã load hết dữ liệu màn hình toppage chưa
	public static final String KEY_LOAD_FINISHED_TOPPAGE = "key_toppage_finished";
	// key để mã hóa/giải mã File
	public static String FILE_SECRET_KEY = "MyDifficultPassw";

	public static String PREFERENCES_SECRET_KEY = "runsystem";
	//
	public static final String ACTION_DOWNLOAD = "download action";
	// Cú pháp shared preferences
	public static String PREFERENCES_DECK_PREFIX = "DECK";

	public static String PREFERENCES_CARD_PREFIX = "CARD";

	// Name shared preferences
	public static String APP_PREFERENCES = "net.oraclecard";

	// Key shared preferences
	public static String CONFIG_XML = "CONFIG_XML";
	//
	public static String PASSWORD = "105af58a06470c1c0d686fdf0134485c";

	public static String URL_XML_INFOAPP = "http://doreen.jp/apnews.xml";

	public static SettingApp getSettingApp(Context context) {
		SettingApp setting = new SettingApp();
		SecurePreferences appPreferences = new SecurePreferences(context,
				APP_PREFERENCES, PREFERENCES_SECRET_KEY, true);
		Gson gson = new Gson();
		Type SettingAppType = new TypeToken<SettingApp>() {
		}.getType();
		if (appPreferences.containsKey(ConstantValue.SETTING_KEY)) {
			String strSetting = appPreferences
					.getString(ConstantValue.SETTING_KEY);
			setting = gson.fromJson(strSetting, SettingAppType);
		}
		return setting;
	}

	public static String getBasePatchApp(Context context) {
		return context.getApplicationContext().getExternalFilesDir(null)
				.getPath();
	}

	public static String getPatchCardData(Context context) {
		return getBasePatchApp(context) + "/Card_Data/";
	}

	public static String getPathCard00(Context context, int deckId) {
		String strDeckId = deckId < 10 ? "0" + deckId : "" + deckId;
		return getPatchCardData(context) + "Card_" + strDeckId + "/en_c"
				+ strDeckId + "d.png";
	}

	public static String getPathDeckImage(Context context, int deckId,
			String prefix) {
		String strDeckId = deckId < 10 ? "0" + deckId : "" + deckId;
		return getPatchCardData(context) + "Card_" + strDeckId + "/en_c"
				+ strDeckId + prefix + ".png";
	}

	public static String getPathBackgroundByDeckId(Context context, int deckId) {
		String strDeckId = deckId < 10 ? "0" + deckId : "" + deckId;
		return getPatchCardData(context) + "Card_" + strDeckId + "/en_bg"
				+ strDeckId + ".jpg";
	}

	public static final int X1 = 1;
	public static final int X2 = 2;
	public static final int X3 = 3;

	public static String getPathCardDetail(Context context, int deckId,
			int cardId, int sizeType) {
		String strDeckId = deckId < 10 ? "0" + deckId : "" + deckId;
		String strCardId = cardId < 10 ? "0" + cardId : "" + cardId;
		String strSize = "";
		switch (sizeType) {
		case X2:
			strSize = "/X2/";
			break;

		case X3:
			strSize = "/X3/";
			break;
		default:
			break;
		}
		String path = getPatchCardData(context) + "Card_" + strDeckId + strSize
				+ "/en_c" + strDeckId + strCardId + ".png";
		return path;
	}

	/**
	 * Check available free space on SD card
	 * 
	 * @return true if free space SD card > required size
	 */
	public static boolean isAvailableSpaceSDCard(int sizeRequiredMB) {
		StatFs stat = new StatFs(Environment.getExternalStorageDirectory()
				.getPath());
		double sdAvailSize = (double) stat.getAvailableBlocks()
				* (double) stat.getBlockSize();

		// One binary MB equals 1024*1024 bytes.
		double minSize = sizeRequiredMB * 1024 * 1024;
		if (sdAvailSize > minSize)
			return true;
		else
			return false;
	}
}